package com.hogocloud.myframe

import android.os.Bundle
import android.view.KeyEvent
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.navigation.NavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.hogocloud.corelibrary.base.BaseActivity
import org.jetbrains.anko.toast

class MainActivity : BaseActivity() {

    private var firstTime: Long = 0
    private var currentNavController: LiveData<NavController>? = null

    override fun initLayout(): Int {
        return R.layout.activity_main
    }

    override fun initView(savedInstanceState: Bundle?) {

        if (savedInstanceState == null) {
            setupBottomNavigationBar()
        } // Else, need to wait for onRestoreInstanceState
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        // Now that BottomNavigationBar has restored its instance state
        // and its selectedItemId, we can proceed with setting up the
        // BottomNavigationBar with Navigation
        setupBottomNavigationBar()
    }

    /**
     * Called on first creation and when restoring state.
     */
    private fun setupBottomNavigationBar() {
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)

        val navGraphIds = listOf(R.navigation.nav_home, R.navigation.nav_dash, R.navigation.nav_mine)

        // Setup the bottom navigation view with a list of navigation graphs
        val controller = bottomNavigationView.setupWithNavController(
            navGraphIds = navGraphIds,
            fragmentManager = supportFragmentManager,
            containerId = R.id.fl_nav_container,
            intent = intent
        )

        // Whenever the selected controller changes, setup the action bar.
        controller.observe(this, Observer { navController ->
            //setupActionBarWithNavController(navController)
        })
        currentNavController = controller
    }

    override fun onSupportNavigateUp(): Boolean {
        return currentNavController?.value?.navigateUp() ?: false
    }

    /**
     * Overriding popBackStack is necessary in this case if the app is started from the deep link.
     */
    override fun onBackPressed() {
        if (currentNavController?.value?.popBackStack() != true) {
            super.onBackPressed()
        }
    }


    /**
     * 返回键二次点击退出
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 再按一次退出应用
            val secondTime = System.currentTimeMillis()
            if (System.currentTimeMillis() - firstTime > 1000) {
                toast(R.string.exit_app)
                firstTime = secondTime
            } else {
                try {
                    //不杀进程，不finish页面
                    moveTaskToBack(false)
                    //AppManager.getAppManager().AppExit(this)
                } catch (e: Exception) {
                    e.printStackTrace()
                }

            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}
